/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
UserWindow = function() {
    this.currentPass = new Ext.form.TextField({
        fieldLabel: 'Current Password',
        emptyText: '',
        width: 260,
        inputType: 'password',
        validationEvent: false,
        validateOnBlur: false,
        msgTarget: 'under',
        triggerAction: 'all',
        mode: 'local',
        listeners:{scope: this}
    });

    this.newPass = new Ext.form.TextField({
        fieldLabel: 'New Password',
        emptyText: '',
        width: 260,
        inputType: 'password',
        validationEvent: false,
        validateOnBlur: false,
        msgTarget: 'under',
        triggerAction: 'all',
        mode: 'local',
        listeners:{scope: this}
    });

    this.confirmPass = new Ext.form.TextField({
        fieldLabel: 'Confirm Password',
        emptyText: '',
        width: 260,
        inputType: 'password',
        validationEvent: false,
        validateOnBlur: false,
        msgTarget: 'under',
        triggerAction: 'all',
        mode: 'local',
        listeners:{scope: this}
    });

    this.form = new Ext.FormPanel({
        labelAlign:'top',
        items: [this.currentPass,this.newPass,this.confirmPass],
        border: false,
        bodyStyle:'background:transparent;padding:10px;'
    });

	UserWindow.superclass.constructor.call(this, {
        title: 'Change Password',
        iconCls: 'password-icon',
        autoHeight: true,
        width: 300,
        resizable: false,
        plain:true,
        modal: true,
        y: 100,
        autoScroll: false,
        closeAction: 'hide',

        buttons:[{
            text: 'Change',
            handler: this.onChange,
            scope: this
        },{
            text: 'Cancel',
            handler: this.hide.createDelegate(this, [])
        }],

        items: this.form
    });
}

Ext.extend(UserWindow, Ext.Window, {
    
    show : function(){
    	this.currentPass.reset();
    	this.newPass.reset();
    	this.confirmPass.reset();
        UserWindow.superclass.show.apply(this, arguments);
    },

    onChange: function() {
		var aserror = 0;

		if(this.currentPass.getValue() == '') {
       		this.currentPass.markInvalid("Invalid pass!");
       		aserror = 1;
		}

		if(this.newPass.getValue() == '') {
       		this.newPass.markInvalid("Invalid new passworld!");
       		aserror = 1;
		}
    	
		if(this.newPass.getValue() != this.confirmPass.getValue()) {
       		this.confirmPass.markInvalid("Passwords do not match!");
       		aserror = 1;
		}

    	if(aserror == 0) {
	        this.el.mask('Changing password...', 'x-mask-loading');
			SGui.changePassword(this.currentPass.getValue(), this.newPass.getValue(), this.validatePassword, this);
		}
    },

    validatePassword : function(obj,result){
    	if(result.length > 1) {
    		if(result[0] == '1') {
                obj.el.unmask();
                obj.hide();
    		} else {
                obj.el.unmask();
    			obj.currentPass.markInvalid(result[1]);
    		}    		
    	} else {
    		alert('Invalid Protocol From Server...');
    	}
	}
});
